/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerSearchUI.java
 *
 * Created on Jan 11, 2011, 3:50:36 PM
 */

package GUILayer;

import ControllerLayer.PersonController;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import ModelLayer.Customer;
import ModelLayer.SalesOrder;


/**
 *
 * @author pelle
 */
public class CustomerSearchUI extends javax.swing.JFrame {

    private PersonController personCtr = PersonController.getInstance();
    private Object parent;
    private ArrayList<Customer> persons;

    /** Creates new form CustomerSearchUI */

    public CustomerSearchUI() {
        initComponents();
        notFound.setVisible(false);
    }

    public CustomerSearchUI(Object parent) {
        initComponents();
        notFound.setVisible(false);
        this.parent = parent;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        customerList = new javax.swing.JList();
        seekField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        notFound = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        selectBtn.setText("Vælg");
        selectBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText("Annuller");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(customerList);

        seekField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                seekFieldKeyReleased(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 24));
        jLabel1.setText("Søg efter kunde");

        jLabel2.setText("Kundenavn:");

        notFound.setForeground(new java.awt.Color(153, 51, 0));
        notFound.setText("Ingen kunder fundet");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(notFound)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 120, Short.MAX_VALUE)
                        .add(cancelBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(selectBtn))
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(seekField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(seekField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(7, 7, 7)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(notFound)
                    .add(selectBtn)
                    .add(cancelBtn))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void seekFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_seekFieldKeyReleased
        searchPerson();
    }//GEN-LAST:event_seekFieldKeyReleased

    private void selectBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectBtnActionPerformed
        try {
            Customer selected = persons.get(customerList.getSelectedIndex());
            if(parent instanceof SalesOrder) {
                SalesOrder oi = (SalesOrder) parent;
                oi.setCustomer(selected);
             }

            dispose();
        } catch(Exception e) {}
    }//GEN-LAST:event_selectBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        dispose();
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void searchPerson() {
        String seek = this.seekField.getText();
        persons = personCtr.searchCustomerByName(seek);

        DefaultListModel listModel = new DefaultListModel();
        
        notFound.setVisible(false);

        if(persons.size() > 0 && seek.length() > 0) {
            for(Customer deb : persons) {
                listModel.addElement(deb.getName() + " (ID: " + deb.getPersonID() + ")");
            }
        } else {
            if(seek.length() > 0) {
                notFound.setVisible(true);
            }
        }

        customerList.removeAll();
        customerList.setModel(listModel);
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CustomerSearchUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JList customerList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel notFound;
    private javax.swing.JTextField seekField;
    private javax.swing.JButton selectBtn;
    // End of variables declaration//GEN-END:variables

}
